<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备编码" prop="machineryCode">
        <el-input v-model="queryParams.machineryCode" placeholder="请输入设备类型编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备名称" prop="machineryName">
        <el-input v-model="queryParams.machineryName" placeholder="请输入设备类型名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="带班" prop="lead">
        <el-input v-model="queryParams.lead" placeholder="请输入带班" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工作单号" prop="workorderCode">
        <el-input v-model="queryParams.workorderCode" placeholder="请输入生产工单编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="生产工单名称" prop="workorderName">
        <el-input v-model="queryParams.workorderName" placeholder="请输入生产工单名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="原纸卷号" prop="rollNumber">
        <el-input v-model="queryParams.rollNumber" placeholder="请输入原纸卷号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="itemCode">
        <el-input v-model="queryParams.itemCode" placeholder="请输入产品物料编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input v-model="queryParams.itemName" placeholder="请输入产品物料名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="助1" prop="assistant">
        <el-input v-model="queryParams.assistant" placeholder="请输入助1" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="班次" prop="workGroup">
        <el-input v-model="queryParams.workGroup" placeholder="请输入班次" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="报表号" prop="reportNo">
        <el-input v-model="queryParams.reportNo" placeholder="请输入报表号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="理论产量" prop="theoreticalYield">
        <el-input v-model="queryParams.theoreticalYield" placeholder="请输入理论产量" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="报工状态" prop="fbStatus">
        <el-select v-model="queryParams.fbStatus" placeholder="请选择报工状态" clearable>
          <el-option
            v-for="dict in dict.type.mes_fb_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['fb:preprinting:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['fb:preprinting:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['fb:preprinting:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['fb:preprinting:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-table v-loading="loading" :data="preprintingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="上报时间" align="center" prop="reportingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="记录ID" align="center" prop="recordId" fixed="left"/>
      <el-table-column label="原纸卷号" align="center" prop="rollNumber" fixed="left" /> -->
      <!-- <el-table-column label="设备类型ID" align="center" prop="machineryId" /> -->
      <el-table-column label="机台号" align="center" prop="machineryCode" fixed="left"/>
      <el-table-column label="带班" align="center" prop="lead" fixed="left"/>
      <el-table-column label="工作单号" align="center" prop="workorderCode" fixed="left"/>
      <el-table-column label="产品编号" align="center" prop="itemCode" />

      <el-table-column label="设备名称" align="center" prop="machineryName" fixed="left" />
      <!-- <el-table-column label="生产工单ID" align="center" prop="workorderId" /> -->
      <el-table-column label="工单名称" align="center" prop="workorderName" fixed="left"/>
      <!-- <el-table-column label="生产任务ID" align="center" prop="taskId" /> -->
      <el-table-column label="生产任务编号" align="center" prop="taskCode" />
      <!-- <el-table-column label="产品物料ID" align="center" prop="itemId" /> -->
      <el-table-column label="产品名称" align="center" prop="itemName" />

      <el-table-column label="截断长" align="center" prop="paperLong" />
      <el-table-column label="预印米数" align="center" prop="preprintingMeters" />
      <el-table-column label="预印张数" align="center" prop="preprintingNumber" />
      <el-table-column label="异常停机超时" align="center" prop="abnormalShutdownTimeout" />
      <el-table-column label="报废重量/KG" align="center" prop="scrapWeight" />
      <el-table-column label="调机换单" align="center" prop="changeOrders" />
      <el-table-column label="打样套数" align="center" prop="sampleNumber" />
      <el-table-column label="当班生产情况" align="center" prop="proSituation" show-overflow-tooltip />
      <el-table-column label="助1" align="center" prop="assistant" />
      <el-table-column label="·" align="center" prop="info" />
      <el-table-column label="每米单重" align="center" prop="meterWeight" />
      <el-table-column label="报废米数" align="center" prop="scrapMeters" />
      <el-table-column label="班次" align="center" prop="workGroup" />
      <el-table-column label="报表号" align="center" prop="reportNo" />
      <el-table-column label="理论产量" align="center" prop="theoreticalYield" />
      <el-table-column label="报工状态" align="center" prop="fbStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_fb_status" :value="scope.row.fbStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width"  fixed="right" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-if="scope.row.fbStatus !== 2"
            v-hasPermi="['fb:preprinting:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-if="scope.row.fbStatus !== 2"
            v-hasPermi="['fb:preprinting:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改预印报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" inline :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="设备类型ID" prop="machineryId">
          <el-input v-model="form.machineryId" placeholder="请输入设备类型ID" />
        </el-form-item> -->
        <el-form-item label="设备编码" prop="machineryCode">
          <!-- <el-input v-model="form.machineryCode" placeholder="请输入设备类型编码" /> -->
          <el-input v-model="form.machineryCode" style="width: 200px;" placeholder="请输入设备编码" readonly="readonly">
            <el-button slot="append" style="width: 30px; padding: 10px;" @click="handleMachineryAdd"
              icon="el-icon-search"></el-button>
          </el-input>
          <MachinerySelectSingle ref="machinerySelect" @onSelected="onMachineryAdd"></MachinerySelectSingle>
        </el-form-item>
        <!-- <el-form-item label="设备名称" prop="machineryName">
          <el-input v-model="form.machineryName" placeholder="请输入设备类型名称" />
        </el-form-item> -->

        <!-- <el-form-item label="生产工单ID" prop="workorderId">
          <el-input v-model="form.workorderId" placeholder="请输入生产工单ID" />
        </el-form-item> -->
        <el-form-item label="生产工单编号" prop="workorderCode">
          <!-- <el-input v-model="form.workorderCode" placeholder="请输入生产工单编号" /> -->
          <el-input v-model="form.workorderCode" style="width: 200px;" placeholder="请输入工单编码">
            <el-button slot="append" style="width: 30px; padding: 10px;" icon="el-icon-search"
              @click="handleWorkorderSelect"></el-button>
          </el-input>
          <WorkorderSelect ref="woSelect" @onSelected="onWorkorderSelected"></WorkorderSelect>
        </el-form-item>
        <!-- <el-form-item label="生产工单名称" prop="workorderName">
          <el-input v-model="form.workorderName" placeholder="请输入生产工单名称" />
        </el-form-item> -->
        <el-form-item label="带班" prop="lead">
          <el-input v-model="form.lead" placeholder="请输入带班" />
        </el-form-item>
        <!-- <el-form-item label="生产任务ID" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入生产任务ID" />
        </el-form-item> -->
        <!-- <el-form-item label="生产任务编号" prop="taskCode">
          <el-input v-model="form.taskCode" placeholder="请输入生产任务编号" />
        </el-form-item> -->
        <!-- <el-form-item label="产品物料ID" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入产品物料ID" />
        </el-form-item> -->
        <!-- <el-form-item label="产品物料编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入产品物料编码" />
        </el-form-item>
        <el-form-item label="产品物料名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入产品物料名称" />
        </el-form-item> -->
        <el-form-item label="截断长" prop="paperLong">
          <el-input v-model="form.paperLong" placeholder="请输入截断长" />
        </el-form-item>
        <el-form-item label="预印米数" prop="preprintingMeters">
          <el-input v-model="form.preprintingMeters" placeholder="请输入预印米数" />
        </el-form-item>
        <el-form-item label="预印张数" prop="preprintingNumber">
          <el-input v-model="form.preprintingNumber" placeholder="请输入预印张数" />
        </el-form-item>
        <el-form-item label="异常停机超时" prop="abnormalShutdownTimeout">
          <el-input v-model="form.abnormalShutdownTimeout" placeholder="请输入异常停机超时" />
        </el-form-item>
        <el-form-item label="报废重量/KG" prop="scrapWeight">
          <el-input v-model="form.scrapWeight" placeholder="请输入报废重量/KG" />
        </el-form-item>
        <el-form-item label="调机换单" prop="changeOrders">
          <el-input v-model="form.changeOrders" placeholder="请输入调机换单" />
        </el-form-item>
        <el-form-item label="打样套数" prop="sampleNumber">
          <el-input v-model="form.sampleNumber" placeholder="请输入打样套数" />
        </el-form-item>
        <el-form-item label="生产情况" prop="proSituation">
          <el-input v-model="form.proSituation" placeholder="请输入生产情况" />
        </el-form-item>
        <el-form-item label="助1" prop="assistant">
          <el-input v-model="form.assistant" placeholder="请输入助1" />
        </el-form-item>
        <el-form-item label="每米单重" prop="meterWeight">
          <el-input v-model="form.meterWeight" placeholder="请输入每米单重" />
        </el-form-item>
        <el-form-item label="报废米数" prop="scrapMeters">
          <el-input v-model="form.scrapMeters" placeholder="请输入报废米数" />
        </el-form-item>
        <el-form-item label="·" prop="info">
          <el-input v-model="form.info" placeholder="请输入·" />
        </el-form-item>
        <el-form-item label="班次" prop="workGroup">
          <el-input v-model="form.workGroup" placeholder="请输入班次" />
        </el-form-item>
        <el-form-item label="报表号" prop="reportNo">
          <el-input v-model="form.reportNo" placeholder="请输入报表号" />
        </el-form-item>
        <el-form-item label="理论产量" prop="theoreticalYield">
          <el-input v-model="form.theoreticalYield" placeholder="请输入理论产量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="数据行状态(0:正常/1:删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入数据行状态(0:正常/1:删除)" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPreprinting,
    getPreprinting,
    delPreprinting,
    addPreprinting,
    updatePreprinting
  } from "@/api/mes/fb/preprinting";
  import { getToken } from "@/utils/auth";
  import MachinerySelectSingle from "@/components/machinerySelect/single.vue";
  import WorkorderSelect from "@/components/workorderSelect/single.vue"

  export default {
    name: "Preprinting",
    dicts: ['mes_fb_status'],
    components: {
      MachinerySelectSingle,
      WorkorderSelect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 预印报表格数据
        preprintingList: [],
        // 时间范围
        daterangeCreateTime: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          machineryId: null,
          machineryCode: null,
          machineryName: null,
          lead: null,
          workorderId: null,
          workorderCode: null,
          workorderName: null,
          taskId: null,
          taskCode: null,
          itemId: null,
          itemCode: null,
          itemName: null,
          paperLong: null,
          preprintingMeters: null,
          preprintingNumber: null,
          abnormalShutdownTimeout: null,
          scrapWeight: null,
          changeOrders: null,
          sampleNumber: null,
          proSituation: null,
          assistant: null,
          meterWeight: null,
          scrapMeters: null,
          info: null,
          workGroup: null,
          reportNo: null,
          theoreticalYield: null,
          fbStatus: null,
          params: {}
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          rollNumber: [{ required: true, message: "原纸卷号不能为空", trigger: "blur" }],
          machineryCode: [{ required: true, message: "机台不能为空", trigger: "blur" }],
          workorderCode: [{ required: true, message: "工作单号不能为空", trigger: "blur" }],
          preprintingNumber: [{ required: true, message: "预印张数不能为空", trigger: "blur" }],
          workGroup: [{ required: true, message: "班次不能为空", trigger: "blur" }],
        },
        // 导入参数
        upload: {
          // 是否显示弹出层
          open: false,
          // 弹出层标题
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: {
            Authorization: "Bearer " + getToken()
          },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/mes/fb/preprinting/importData"
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询预印报列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
          this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
          this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
        }
        listPreprinting(this.queryParams).then(response => {
          this.preprintingList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 生产工单选择弹出
      handleWorkorderSelect() {
        this.$refs.woSelect.showFlag = true;
      },
      // 生产工单选择回调
      onWorkorderSelected(row) {
        if (row != undefined && row != null) {
          this.form.workorderId = row.workorderId;
          this.form.workorderCode = row.workorderCode;
          this.form.workorderName = row.workorderName;
        }
      },
      //设备资源选择弹出
      handleMachineryAdd() {
        this.$refs.machinerySelect.showFlag = true;
      },
      //设备资源选择回调
      onMachineryAdd(row) {
        if (row != undefined && row != null) {
          this.form.machineryId = row.machineryId;
          this.form.machineryCode = row.machineryCode;
          this.form.machineryName = row.machineryName;
        }
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "报表导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        this.download('/mes/fb/preprinting/importTemplate', {}, `fbReel_template_${new Date().getTime()}.xlsx`)
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response
          .msg + "</div>", "导入结果", {
            dangerouslyUseHTMLString: true
          });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          recordId: null,
          machineryId: null,
          machineryCode: null,
          machineryName: null,
          lead: null,
          workorderId: null,
          workorderCode: null,
          workorderName: null,
          taskId: null,
          taskCode: null,
          itemId: null,
          itemCode: null,
          itemName: null,
          paperLong: null,
          preprintingMeters: null,
          preprintingNumber: null,
          abnormalShutdownTimeout: null,
          scrapWeight: null,
          changeOrders: null,
          sampleNumber: null,
          proSituation: null,
          assistant: null,
          meterWeight: null,
          scrapMeters: null,
          info: null,
          workGroup: null,
          reportNo: null,
          theoreticalYield: null,
          remark: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.recordId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加预印报";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const recordId = row.recordId || this.ids
        getPreprinting(recordId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改预印报";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.recordId != null) {
              updatePreprinting(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPreprinting(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const recordIds = row.recordId || this.ids;
        this.$modal.confirm('是否确认删除预印报编号为"' + recordIds + '"的数据项？').then(function() {
          return delPreprinting(recordIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('/mes/fb/preprinting/export', {
          ...this.queryParams
        }, `preprinting_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
